package it.ipermemory.graphic;

import it.ipermemory.R;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Defines a new custom pop-up
 * 
 * @author Gianluca Carlesso (gianluca.carlesso@gmail.com)
 * @version 1.0
 * 
 */
public abstract class CustomPopup extends Dialog implements OnClickListener {

	/**
	 * Context of application
	 */
	private Context context;

	/**
	 * Text of pop-up
	 */
	private TextView textMessage;

	/**
	 * Button "YES" of pop-up
	 */
	private Button yesButton;

	/**
	 * Button "NO" of pop-up
	 */
	private Button noButton;

	/**
	 * Object for the preparation of a custom font
	 */
	private Typeface typface;

	/**
	 * Initialize a new object pop-up
	 * 
	 * @param cotext
	 *            context of application
	 * @param text
	 *            text to be displayed in the pop-up
	 */
	public CustomPopup(Context cotext, String text) {
		super(cotext, android.R.style.Theme_Translucent_NoTitleBar);

		context = cotext;

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.custom_popup);

		// Customize the font of buttons and text
		typface = Typeface.createFromAsset(context.getAssets(),
				"fonts/wayoshi.ttf");

		textMessage = (TextView) findViewById(R.id.TextMessage);
		textMessage.setTypeface(typface);
		textMessage.setText(text);

		yesButton = (Button) findViewById(R.id.OkButton);
		yesButton.setTypeface(typface);
		yesButton.setOnClickListener(this);

		noButton = (Button) findViewById(R.id.NoButton);
		noButton.setTypeface(typface);
		noButton.setOnClickListener(this);

		// I prevent the deletion of the pop-up without selecting an
		setCanceledOnTouchOutside(false);
		setCancelable(false);
	}

	/**
	 * Returns the message text
	 * 
	 * @return a text view of message
	 */
	public TextView getTextMessage() {
		return textMessage;
	}

	/**
	 * Sets the object to display text in a pop-up
	 * 
	 * @param textMessage
	 *            text to be displayed
	 */
	public void setTextMessage(TextView textMessage) {
		this.textMessage = textMessage;
	}

	/**
	 * Returns the YES button
	 * 
	 * @return a button that represent the YES
	 */
	public Button getYesButton() {
		return yesButton;
	}

	/**
	 * Sets the object that represents the button YES
	 * 
	 * @param yesButton
	 *            button that represents the button YES
	 */
	public void setYesButton(Button yesButton) {
		this.yesButton = yesButton;
	}

	/**
	 * Returns the NO button
	 * 
	 * @return a button that represent the NO
	 */
	public Button getNoButton() {
		return noButton;
	}

	/**
	 * Sets the object that represents the button NO
	 * 
	 * @param noButton
	 *            button that represents the button NO
	 */
	public void setNoButton(Button noButton) {
		this.noButton = noButton;
	}

	/**
	 * Call the method that will handle the link in the pop-up window, forcing
	 * the redefinition
	 */
	public abstract void onClick(View v);

	/**
	 * Set the background of the pop-up
	 * 
	 * @param background
	 *            object that defines the background of the popup
	 */
	@SuppressWarnings("deprecation")
	public void setBackgroundDrawable(Drawable background) {
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.custom_popup_layout);

		// Backward compatibility to API previous 16
		linearLayout.setBackgroundDrawable(background);
	}

}
